//
//  HomeController.m
//  Mango
//
//  Created by lanou3g on 16/5/4.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "HomeController.h"
#import "UIView+Extension.h"
#import "HomeHeader.h"

@interface HomeController ()<UIScrollViewDelegate>
/** 标签栏底部的红色指示器 */
@property (nonatomic, weak) UIView *indicatorView;
/** 当前选中的按钮 */
@property (nonatomic, weak) UIButton *selectedButton;
/** 顶部的所有标签 */
@property (nonatomic, weak) UIScrollView *titlesView;
/** 底部的所有内容 */
@property (nonatomic, weak) UIScrollView *contentView;

@property (nonatomic, copy) NSString *currentSkinModel;
@end

@implementation HomeController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    //初始化所有子控制器
    [self setupChildViewControllers];
    //设置标题栏
    [self setupTitlesView];
    
    //设置内容contentView
    [self setupContentView];
    //不要自动调整inset
    self.automaticallyAdjustsScrollViewInsets = NO;
    //开始默认显示第一个界面
    [self scrollViewDidEndScrollingAnimation:self.contentView];
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateSkinModel) name:SkinModelDidChangedNotification object:nil];
    [self updateSkinModel];
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark --private Method--更新皮肤模式 接到模式切换的通知后会调用此方法
-(void)updateSkinModel {
    self.currentSkinModel = [[NSUserDefaults standardUserDefaults] stringForKey:CurrentSkinModelKey];
    if ([self.currentSkinModel isEqualToString:NightSkinModelValue]) {
        self.view.backgroundColor = [UIColor blackColor];
        self.titlesView.backgroundColor = [UIColor colorWithRed:34/255.0 green:30/255.0 blue:33/255.0 alpha:1.0];
        self.titlesView.backgroundColor = [UIColor colorWithRed:34/255.0 green:30/255.0 blue:33/255.0 alpha:1.0];
        for (UIView *view in self.titlesView.subviews) {
            if ([view isKindOfClass:[UIControl class]]) {//是按钮
                UIButton *button = (UIButton *)view;
                [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            }
        }
    } else {//日间模式
        self.view.backgroundColor = [UIColor colorWithRed:250.0/255.0 green:250.0/255.0 blue:250.0/255.0 alpha:1.0];
        self.titlesView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.9];
        self.titlesView.backgroundColor = [UIColor whiteColor];
        for (UIView *view in self.titlesView.subviews) {
            if ([view isKindOfClass:[UIControl class]]) {//是按钮
                UIButton *button = (UIButton *)view;
                [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            }
        }
    }
}




/**
 * 设置顶部的标签栏
 */
- (void)setupTitlesView
{
    // 标签栏整体
    UIScrollView *titlesView = [[UIScrollView alloc] init];
    titlesView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.9];
    titlesView.frame = CGRectMake(0, 64, self.view.bounds.size.width, 36);
    titlesView.contentSize = CGSizeMake(450, 0);
    titlesView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:titlesView];
    self.titlesView = titlesView;
    
    // 底部的红色指示器
    UIView *indicatorView = [[UIView alloc] init];
    indicatorView.backgroundColor = [UIColor redColor];
    indicatorView.height = 2;
    indicatorView.y = titlesView.height - indicatorView.height;
    
    indicatorView.tag = -1;
    self.indicatorView = indicatorView;
    
    // 内部的子标签
    NSArray *titles = @[@"动态",@"八卦",@"时尚",@"生活",@"影视",@"互动",@"视频",@"动图",@"美妆"];
    CGFloat width = 50;
    CGFloat height = titlesView.frame.size.height;
    for (int i = 0; i < titles.count; i++) {
        UIButton *button = [UIButton buttonWithType:(UIButtonTypeRoundedRect)];
        button.frame = CGRectMake(i * width, 0, width, height);
        button.tag = i;
        [button setTitle:titles[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor redColor] forState:(UIControlStateDisabled)];
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        [button addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
        [titlesView addSubview:button];
        
        // 默认点击了第一个按钮
        if (i == 0) {
            button.enabled = NO;
            self.selectedButton = button;
            // 让按钮内部的label根据文字内容来计算尺寸
            [button.titleLabel sizeToFit];
            self.indicatorView.width = button.titleLabel.width;
            self.indicatorView.centerX = button.centerX;

        }
    }
    [titlesView addSubview:indicatorView];
}

/**
 *  button按钮的点击事件
 */
- (void)titleClick:(UIButton *)button
{
    // 修改按钮状态
    self.selectedButton.enabled = YES;
    button.enabled = NO;
    self.selectedButton = button;
    
    // 动画
    [UIView animateWithDuration:0.25 animations:^{
        self.indicatorView.width = button.titleLabel.width;
        self.indicatorView.centerX = button.centerX;
    }];
    
    // 滚动
    CGPoint offset = self.contentView.contentOffset;
    offset.x = button.tag * self.contentView.width;
    [self.contentView setContentOffset:offset animated:YES];
}

/**
 *  设置内容scrollView
 */
- (void)setupContentView {
    UIScrollView *contentView = [[UIScrollView alloc]init];
    contentView.frame = self.view.bounds;
    contentView.delegate = self;
    contentView.pagingEnabled = YES;
    [self.view insertSubview:contentView atIndex:0];
    contentView.contentSize = CGSizeMake(contentView.width * self.childViewControllers.count, 0);
    self.contentView = contentView;
}

/**
 *  初始化所有子控制器
 */
- (void)setupChildViewControllers {
    
    RecommendController *recommendVC = [[RecommendController alloc]init];
    [self addChildViewController:recommendVC];
    
    GossipController *gossipVC = [[GossipController alloc]init];
    [self addChildViewController:gossipVC];
    
    FashionController *fashionVC = [[FashionController alloc]init];
    [self addChildViewController:fashionVC];
    
    LifeController *lifeVC = [[LifeController alloc]init];
    [self addChildViewController:lifeVC];
    
    FilmController *filmVC = [[FilmController alloc]init];
    [self addChildViewController:filmVC];
    
    InteractController *interactVC = [[InteractController alloc]init];
    [self addChildViewController:interactVC];
    
    VideoController *videoVC = [[VideoController alloc]init];
    [self addChildViewController:videoVC];
    
    GifController *gifVC = [[GifController alloc]init];
    [self addChildViewController:gifVC];
    
    BeautyController *beautyVC = [[BeautyController alloc]init];
    [self addChildViewController:beautyVC];
}


#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    // 当前的索引
    NSInteger index = scrollView.contentOffset.x / scrollView.width;
    
    // 取出子控制器
    UITableViewController *vc = self.childViewControllers[index];
    vc.view.x = scrollView.contentOffset.x;
    vc.view.y = 0; // 设置控制器view的y值为0(默认是20)
    vc.view.height = scrollView.height; // 设置控制器view的height值为整个屏幕的高度(默认是比屏幕高度少个20)
    // 设置内边距
    CGFloat bottom = self.tabBarController.tabBar.height;
    CGFloat top = CGRectGetMaxY(self.titlesView.frame);
    vc.tableView.contentInset = UIEdgeInsetsMake(top, 0, bottom, 0);
    // 设置滚动条的内边距
    vc.tableView.scrollIndicatorInsets = vc.tableView.contentInset;
    [scrollView addSubview:vc.view];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self scrollViewDidEndScrollingAnimation:scrollView];
    
    // 点击按钮
    NSInteger index = scrollView.contentOffset.x / scrollView.width;
    [self titleClick:self.titlesView.subviews[index]];
}

@end
